<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>类别管理</title>
<load href="__PUBLIC__/Css/admin_style.css"/>
<load href="__PUBLIC__/Js/jquery.js"/>
<script language="javascript">
   var option_elements="";
   
   var left_selectedIndex=-1;
   var right_selectedIndex=-1;
   //获取子类别
   var get_child_option=function(v){
	     $.ajax({
		   type:"GET",
		   cache:false,
		   async:false,
		   url:"__URL__/get_child",
		   dataType:"json",
		   data:"parid="+parseInt(v),
		   success:function(msg){
			  var msg=eval(msg);
			  $("#child_select").html("");
			  if(msg!==null){	
			    $.each(msg,function(i,n){
				   $("#child_select").append("<option value='"+n.id+"'>"+n.col_name+"</option>"); 
                });
			  }
		   },
		   error:function(msg){
			  alert('出现错误');
			  return false;
		   }
		 });
   };
  
   //加载父类别
   var flush_parid=function(){
	   $.ajax({
		  type:"GET",
		  cache:false,
		  asysnc:false,
		  data:"html",
		  url:"__URL__/get_parid",
		  success:function(msg){
			  $("#all_type").html("<option value='0'>顶级分类</option>");
			  $("#all_type").append(msg);
		  },
		  error:function(){
			alert('出现错误');
			return false;
		  }
	   });
   }
   var edit_typename=function(typeid){
	  if(typeid==0)return false;
	  $.ajax({
		 type:"GET",
		 cache:false,
		 async:false,
		 data:"typeid="+typeid,
		 dataType:"text",
		 url:"__URL__/gettypename2typeid",
		 success:function(msg){
			 $("#edit_typename").val(msg); 
		 }
	  });
   }
$(function(){
	flush_parid();
    //添加子类别
   $("#btn1").bind("click",function(event){
	   if(document.getElementById("all_type").selectedIndex==-1&&document.getElementById('child_select').selectedIndex==-1){
		  alert('请选择父类别');
		  return false;
	   }
	   if($("#typename").val()==""||$("#typename").val()=="请输入类别名称"){
		  alert('类别名称不能为空');
		  return false;
	   }
	   var parid=$("#all_type").val()?$("#all_type").val():$("#child_select").val();
	   var typename=$("#typename").val();
	   
	   $.ajax({
		  type:"GET",
		  cache:false,
		  asysnc:false,
		  data:"text",
		  url:"__URL__/save_child/parid/"+parseInt(parid)+"/typename/"+encodeURIComponent(typename),
		  success:function(msg){
			  if(msg==1){
				 //刷新父类别
			     flush_parid();
			     //刷新子类别
			     get_child_option(parid);  
			  }
			  
		  },
		  error:function(){
			alert('出现错误');
			return false;
		  }
	   });
   });
      //保存修改后的类别名称
   $("#btn2").bind("click",function(){
	    var typeid=all_type.options[left_selectedIndex].value;
		var old_type_name=all_type.options[left_selectedIndex].text.toString().replace("┣","");
		var new_type_name=$("#edit_typename").val();
		if(new_type_name==""||typeid==""){alert('请选择左侧类别');all_type.selectedIndex=0;}
		if(new_type_name==old_type_name)
			return false;
		$.ajax({
			type:"POST",
			cache:false,
			async:false,
			dataType:"text",
			data:"typeid="+typeid+"&new_type_name="+new_type_name,
			url:"__URL__/updatetypename",
			success:function(msg){
			  	if(msg==1){
				  flush_parid();
				}else if(msg==0){
				  alert('出现错误');
				  return false;	
				}
			}
		});
   });
   //向上
   $("#btn_up").bind("click",function(){
	   if(child_select.options.length==0){alert('请选择左侧大类别');return false;}
	   if(child_select.selectedIndex==0)return false;
	   if(child_select.selectedIndex==-1){alert('请选择右侧小类别');return false;}
	   all_type.selectedIndex=-1;
	   
	   var typeid=child_select.options[child_select.selectedIndex].value;
	   $.ajax({
		  type:"GET",
		  cach:false,
		  async:false,
		  dataType:"text",
		  data:"typeid="+typeid,
		  url:"__URL__/up",
		  success:function(msg){
			  var left_value=all_type.options[left_selectedIndex].value;
			  get_child_option(left_value);
			  right_selectedIndex--;
			  child_select.selectedIndex=right_selectedIndex;
			  flush_parid();
		  }
	   });
   });
   //向下
   $("#btn_down").bind("click",function(){
	   if(child_select.options.length==0){alert('请选择左侧大类别');return false;}
	   if(child_select.selectedIndex==child_select.options.length-1)return false;
	   if(child_select.selectedIndex==-1){alert('请选择右侧小类别');return false;}
	   all_type.selectedIndex=-1;   
	   
	   var typeid=child_select.options[child_select.selectedIndex].value;
	   $.ajax({
		  type:"GET",
		  cach:false,
		  async:false,
		  dataType:"text",
		  data:"typeid="+typeid,
		  url:"__URL__/down",
		  success:function(msg){
			  var left_value=all_type.options[left_selectedIndex].value;
			  get_child_option(left_value);
			  right_selectedIndex++;
			  child_select.selectedIndex=right_selectedIndex;
			  flush_parid();
		  }
	   });
   });
});
   
  

</script>
</head>

<body>
<table width="600" border="0" cellspacing="0" cellpadding="0" class="table_class1">
  <tr>
    <td width="200px;"><select name="all_type" id="all_type" size="30" style="width:200px;" onchange="get_child_option(this.value);edit_typename(this.value);left_selectedIndex=this.selectedIndex;">
    </select></td>
    <td width="250px;" align="center" valign="middle"><input type="button" name="btn_up" id="btn_up" value="↑" /><input type="button" name="btn_down" id="btn_down" value="↓" />
 <br /><input type="text" id="edit_typename" name="edit_typename" style="width:160px;"/><input type="button" name="btn2" id="btn2" value="修改类别" />
 
<input name="typename" type="text" id="typename" style="width:160px;" value="请输入类别名称"  onmousedown="this.value=''" onblur="if(this.value==''){this.value='请输入类别名称';}" /><input type="button" name="btn1" id="btn1" value="添加子类" />
 </td>
    <td width="300px;"><select  onchange="document.getElementById('all_type').selectedIndex=-1;right_selectedIndex=this.selectedIndex;" name="child_select" id="child_select" size="30" style="width:200px;">
</select></td>
  </tr>
</table>
</body>
</html>
